package com.ruoyi.mes.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mes.domain.HouInfo;
import com.ruoyi.mes.domain.LocatorInfo;
import com.ruoyi.mes.mapper.HouInfoMapper;
import com.ruoyi.mes.mapper.LocatorInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mes.mapper.ProStockMapper;
import com.ruoyi.mes.domain.ProStock;
import com.ruoyi.mes.service.IProStockService;

/**
 * 产品库存Service业务层处理
 *
 * @author LongAndHao
 * @date 2023-04-04
 */
@Service
public class ProStockServiceImpl implements IProStockService
{
    @Autowired
    private HouInfoMapper houInfoMapper;

    @Autowired
    private ProStockMapper proStockMapper;

    @Autowired
    private LocatorInfoMapper locatorInfoMapper;

    /**
     * 查询产品库存
     *
     * @param id 产品库存主键
     * @return 产品库存
     */
    @Override
    public ProStock selectProStockById(Long id)
    {
        return proStockMapper.selectProStockById(id);
    }

    /**
     * 查询产品库存列表
     *
     * @param proStock 产品库存
     * @return 产品库存
     */
    @Override
    public List<ProStock> selectProStockList(ProStock proStock)
    {
        List<ProStock> proStocks = proStockMapper.selectProStockList(proStock);
        for (ProStock stock : proStocks) {
            HouInfo houInfo = houInfoMapper.selectHouInfoByHouId(stock.getHouId());
            stock.setHouName(houInfo.getHouName());
            stock.setHouRemainCapacity(houInfo.getHouRemainCapacity());
        }
        return proStocks;
    }

    /**
     * 新增产品库存
     *
     * @param proStock 产品库存
     * @return 结果
     */
    @Override
    public AjaxResult insertProStock(ProStock proStock)
    {
        //新增时判断库房剩余容量是否充足
        HouInfo houInfo = houInfoMapper.selectHouInfoByHouId(proStock.getHouId());
        if (houInfo.getHouRemainCapacity() < proStock.getProQuantity()){
            return AjaxResult.error("库房剩余容量不足!");
        }

        houInfo.setHouRemainCapacity(houInfo.getHouRemainCapacity() - proStock.getProQuantity());
        houInfoMapper.updateHouInfo(houInfo);

        proStock.setCreateTime(DateUtils.getNowDate());
        proStockMapper.insertProStock(proStock);
        return AjaxResult.success("新增成功!");
    }

    /**
     * 修改产品库存
     *
     * @param proStock 产品库存
     * @return 结果
     */
    @Override
    public int updateProStock(ProStock proStock)
    {
        proStock.setUpdateTime(DateUtils.getNowDate());
        return proStockMapper.updateProStock(proStock);
    }

    /**
     * 批量删除产品库存
     *
     * @param ids 需要删除的产品库存主键
     * @return 结果
     */
    @Override
    public int deleteProStockByIds(Long[] ids)
    {
        return proStockMapper.deleteProStockByIds(ids);
    }

    /**
     * 删除产品库存信息
     *
     * @param id 产品库存主键
     * @return 结果
     */
    @Override
    public int deleteProStockById(Long id)
    {
        return proStockMapper.deleteProStockById(id);
    }
}
